

function is_weixin() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}


export async function handerLogin() {
    // 判断是否是微信浏览器
    let flag = is_weixin();
    if (!flag) {
        uni.showToast({
            icon: "error",
            title: "请使用微信内置浏览器"
        })
        return
    }
    let code = getUrlParam("code"); // 截取路径中的code
    console.log(code)
    if (code == null || code === "") {
        // 获取回掉地址
        const res = await getOfficialAccountInformationApi()
        const callBack = "http://mobi.rmbtest.cn/#/"
        // const callBack = 'http://localhost:8080/#/'; // 测试回掉地址1
        const appID = res.datas.public_appid; //公众号appID
        // const appID = 'wx285bd0431917696f' // 测试appID
        const BaseUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appID + '&redirect_uri=' +
            encodeURIComponent(callBack) + '&response_type=code&scope=snsapi_base#wechat_redirect'
        window.location.href = BaseUrl;
    }
    if (code != "" && code != null) {
        const userInfo = await getPublicLogin({
            code
        })
        if (userInfo.code == 200) {
            uni.setStorageSync("token", userInfo.datas.token)
            if (userInfo.datas.is_register == 2) { // 没注册前往注册
                uni.showModal({
                    title: "提醒",
                    content: "还没有注册，是否前往注册",
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            uni.navigateTo({
                                url: "/pages/register/register"
                            })
                        }
                    }
                })
            }
        }
    }
}